package com.office.meetingManage.meeting.controller;


import com.office.meetingManage.meeting.model.*;
import com.office.meetingManage.meeting.service.IMeetingService;
import com.office.util.DateFormat;
import com.office.util.PageModel;
//import com.sun.deploy.net.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/meeting")
public class MeetingAction {


    @Autowired
    private IMeetingService meetingService;

    public IMeetingService getMeetingService() {
        return meetingService;
    }

    public void setMeetingService(IMeetingService meetingService) {
        this.meetingService = meetingService;
    }


    //查询会议信息
    @RequestMapping("/message")
    @ResponseBody
    public PageModel<MeetingMessage> getMeetingMessage(MeetingMessageCondition meetingMessageCondition, PageModel<MeetingMessage> pageModel){
        String title = meetingMessageCondition.getTitle();//标题
        String meetRoomPlace = meetingMessageCondition.getMeetRoomPlace();//会议室地点
        String name = meetingMessageCondition.getName();//主持人
        if ("%".equals(title)||"_".equals(title)){
            meetingMessageCondition.setTitle(" ");
        }
        if ("%".equals(meetRoomPlace)||"_".equals(meetRoomPlace)){
            meetingMessageCondition.setMeetRoomPlace(" ");
        }
        if ("%".equals(name)||"_".equals(name)){
            meetingMessageCondition.setName(" ");
        }
        return meetingService.getMeetingMessage(meetingMessageCondition,pageModel);
    }

    //查询员工id和姓名
    @RequestMapping("/staff")
    @ResponseBody
    public List<staff> getStaffMessage(){
        return meetingService.getStaffMessage();
    }

    //查询会议室id和会议室名
    @RequestMapping("/meetingRoom")
    @ResponseBody
    public List<meetingRoom> getMeetingRoom(){
        return meetingService.getMeetingRoom();
    }

    //添加会议
    @RequestMapping("/addMeeting")
    @ResponseBody
    public boolean addMeetingMessage(HttpServletRequest request,MeetingMessage meetingMessage){
        String[] arr = (String[])request.getParameterValues("arr[]");//参会人id数组
        String title = request.getParameter("title");//会议主题
        Integer sporsonid = Integer.parseInt(request.getParameter("sporsonid"));//会议发起人id
        String sender = request.getParameter("sender");//发起人姓名

        List<attend> attendList =new ArrayList<>();//参会表数据
        List<OaOnlineMessage> onlineMessageList =new ArrayList<>();//在线信息表
        for (int i = 0; i < arr.length; i++) {
            attendList.add(new attend(Integer.parseInt(arr[i]),0));
            onlineMessageList.add(new OaOnlineMessage(title,sporsonid,sporsonid,Integer.parseInt(arr[i])));
        }

        boolean b = meetingService.addMeetingMessage(meetingMessage);

        if (b){
            boolean b1=false;
            boolean b2=false;
            boolean b3=false;
            for (attend attend:attendList){
                 b1 = meetingService.addAttendStaff(attend);
            }
            if (b1){
                b3=  meetingService.updateMeetRoomStatus(meetingMessage);
                for (OaOnlineMessage oaOnlineMessage:onlineMessageList){
                     b2 = meetingService.addOnlineMessage(oaOnlineMessage);
                }
            }

            return b2;
        }
        return b;
    }


    //修改会议信息

    @RequestMapping("/updateMeeting")
    @ResponseBody
    public boolean updateMeetingMessage(MeetingMessage meetingMessage){
        boolean b = meetingService.updateMeetingMessage(meetingMessage);
        return b;
    }

    //删除会议
    @RequestMapping("/deleteMeeting")
    @ResponseBody
    public boolean deleteMeeting(MeetingMessageCondition meetingMessageCondition){
        boolean flag=false;
        boolean b = meetingService.deleteMeetingMessage(meetingMessageCondition);//删除会议信息
        boolean b1 = meetingService.deleteAttend(meetingMessageCondition);//删除参会人
        boolean b2 = meetingService.deleteMessage(meetingMessageCondition);//删除在线信息
        if (b&&b1){
            flag=true;
        }
        return flag;
    }


    //添加会议记录
    @RequestMapping("/addOaRecord")
    @ResponseBody
    public boolean addOaRecord(OaRecord oaRecord,MeetingMessageCondition meetingMessageCondition){
        List<MeetingMessage> meetingMessage = meetingService.getMeetingMessage(meetingMessageCondition);
        MeetingMessage meetingMessage1 = meetingMessage.get(0);
        Date entime = meetingMessage1.getEntime();//会议结束时间
        Date date = new Date();//当前时间
        List<OaRecord> oaRecords = meetingService.checkOaRecord(oaRecord);
        boolean b=false;
        if (date.compareTo(entime)>0&&oaRecords.size()==0){//比较会议结束时间，大于会议结束时间且会议记录表没有该会议记录
            b= meetingService.addOaRecord(oaRecord);
        }
        return b;
    }


    //查询会议内容
    @RequestMapping("/checkRecordMessage")
    @ResponseBody
    public RecordMessage checkRecordMessage(OaRecord oaRecord){
        RecordMessage recordMessage = meetingService.checkRecordMessage(oaRecord);
        return recordMessage;
    }

    //查询个人安排
    @RequestMapping("/checkPersonalArrangement")
    @ResponseBody
    public List<PersonalArrangement> checkPersonalArrangement(staff staff){


        List<PersonalArrangement> personalArrangements = meetingService.checkPersonalArrangement(staff);
        List<PersonalArrangement> arr=new ArrayList<PersonalArrangement>();
        for (int i = 0; i <personalArrangements.size(); i++) {

            Date date = new Date();
            PersonalArrangement  personalArrangement = personalArrangements.get(i);

            Date  time = personalArrangement.getTime();

             if (date.compareTo(time)<0){
//                 personalArrangements.remove(i);
                 arr.add(personalArrangement);
             }
        }

        return arr;
    }


    //查看留言信息
    @RequestMapping("/checkMessageArrangement")
    @ResponseBody
    public List<MessageArrangement> checkMessageArrangement(staff staff){
        return meetingService.checkMessageArrangement(staff);
    }


    //查看日程安排

    @RequestMapping("/checkWorkArrangement")
    @ResponseBody
    public List<WorkArrangement> checkWorkArrangement(staff staff){
        List<WorkArrangement> workArrangements = meetingService.checkWorkArrangement(staff);
        List<WorkArrangement> workArrangementList = new ArrayList<>();

        for (WorkArrangement workArrangement:workArrangements){
            Date date = new Date();//当前时间
            Date scheduledate = workArrangement.getScheduledate();
            if (scheduledate.compareTo(date)>0){
                workArrangementList.add(workArrangement);
            }
        }
        return workArrangementList;
    }

    //修改留言 已读
    @RequestMapping("/updateMessageArrangement")
    @ResponseBody
    public boolean updateMessageArrangement(MessageArrangement messageArrangement,HttpServletRequest request){

        String[] parameterValues = request.getParameterValues("arr[]");//留言表id数组
        List<MessageArrangement> messageArrangementList = new ArrayList<>();
        for (int i = 0; i < parameterValues.length; i++) {
            messageArrangementList.add(new MessageArrangement(Integer.parseInt(parameterValues[i]),null,null));
        }

        boolean b=false;
        for (MessageArrangement messageArrangement1:messageArrangementList){
             b = meetingService.updateMessageArrangement(messageArrangement1);
        }

        return b;

    }


    //修改参会状态 已参会
    @RequestMapping("/updatePersonalArrangement")
    @ResponseBody
    public boolean updatePersonalArrangement(PersonalArrangement personalArrangement,HttpServletRequest request){
        String[] parameterValues = request.getParameterValues("arrid[]");//参会表id数组
        List<PersonalArrangement> personalArrangementList = new ArrayList<>();

        for (int i = 0; i < parameterValues.length; i++) {
            personalArrangementList.add(new PersonalArrangement(Integer.parseInt(parameterValues[i]),null,null,null,null,null));
        }
        boolean flag=false;
        for (PersonalArrangement personalArrangement1:personalArrangementList){
            flag= meetingService.updatePersonalArrangement(personalArrangement1);
        }

        return flag;
    }



    //删除留言信息安排任务
    @RequestMapping("/deleteMessageArrangement")
    @ResponseBody
    public boolean deleteMessageArrangement(MessageArrangement messageArrangement){
        boolean b = meetingService.deleteMessageArrangement(messageArrangement);
        return b;
    }

    //删除会议安排任务
    @RequestMapping("/deletePersonalArrangement")
    @ResponseBody
    public boolean deletePersonalArrangement(PersonalArrangement personalArrangement){
        return meetingService.deletePersonalArrangement(personalArrangement);
    }

    //修改日程安排 已完成
    @RequestMapping("/updateWorkArrangement")
    @ResponseBody
    public boolean updateWorkArrangement(WorkArrangement workArrangement,HttpServletRequest request){
        String[] parameterValues = request.getParameterValues("arr[]");
        List<WorkArrangement> workArrangementList = new ArrayList<>();

        for (int i = 0; i < parameterValues.length; i++) {
            workArrangementList.add(new WorkArrangement(Integer.parseInt(parameterValues[i]),4,null,4,null));
        }
        boolean b=false;
        for (WorkArrangement workArrangement1:workArrangementList){
            b = meetingService.updateWorkArrangement(workArrangement1);
        }
        return b;
    }

    //删除日程安排任务
    @RequestMapping("/deleteWorkArrangement")
    @ResponseBody
    public boolean deleteWorkArrangement(WorkArrangement workArrangement){
        boolean b = meetingService.deleteWorkArrangement(workArrangement);
        return b;
    }
    
}
